﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Eff.Extensions;

namespace Redmine.Core.Libs.Redmine
{
    public class ThemeModule
    {
        static ThemeModule()
        {
            InstalledThemes = ScanThemes;
        }

        private static IEnumerable<Theme> InstalledThemes;
        public static IEnumerable<Theme> Themes
        {
            get
            {
                return InstalledThemes;
            }
        }
        public static void Rescan()
        {
            InstalledThemes = ScanThemes;
        }

        public static Theme GetTheme(string name, bool rescan = false)
        {
            if (name.IsEmptyOrNull())
                return null;
            var theme = Themes.Where(t => t.Equals(name)).FirstOrDefault();
            if (theme == null && rescan)
            {
                Rescan();
                theme = GetTheme(name);
            }
            return theme;
        }
        private static readonly string ThemePath = System.Environment.CurrentDirectory + "\\Content\\themes\\";
        private static IEnumerable<Theme> ScanThemes
        {
            get
            {
                var dirs = Directory.GetDirectories(ThemePath, "*", SearchOption.TopDirectoryOnly).Where(
                         t => File.Exists(ThemePath + t + "\\stylesheets\\application.css")).OrderBy(t => t);
                return dirs.Select(t => new Theme(t));
            }
        }
    }

    public class Theme
    {
        public string Path { get; private set; }
        //   public DirectoryInfo Dir { get; private set; }
        private IEnumerable<string> _stylesheets;
        private IEnumerable<string> _javascripts;
        public string DirName { get; private set; }
        public Theme(string path)
        {
            Path = path;
            DirName = new DirectoryInfo(path).Name;
            _stylesheets = null;
            _javascripts = null;
        }
        public bool Equals(Theme theme)
        {
            return theme != null && theme.DirName == DirName;
        }

        //         def stylesheets
        //        @stylesheets ||= assets("stylesheets", "css")
        //      end
        //      
        //      def javascripts
        //        @javascripts ||= assets("javascripts", "js")
        //      end
        //      
        //      def stylesheet_path(source)
        //        "/themes/#{dir}/stylesheets/#{source}"
        //      end
        //      
        //      def javascript_path(source)
        //        "/themes/#{dir}/javascripts/#{source}"
        //      end

        public IEnumerable<string> Stylesheets
        {
            get
            {
                return _stylesheets ?? (_stylesheets = Assets("stylesheets", "css"));
            }
        }

        public IEnumerable<string> Javascripts
        {
            get
            {
                return _javascripts ?? (_javascripts = Assets("javascripts", "js"));
            }
        }

        private const string PathFormat = "/themes/{0}/{1}/{2}";
        public string GetStylesheetPath(string source)
        {
            return string.Format(PathFormat, DirName, "stylesheets", source);
        }

        public string GetJavascriptPath(string source)
        {
            return string.Format(PathFormat, DirName, "javascripts", source);
        }

        private IEnumerable<string> Assets(string dir, string ext)
        {
            //Dir.glob("#{path}/#{dir}/*.#{ext}").collect {|f| File.basename(f).gsub(/\.#{ext}$/, '')}
            return
                Directory.GetFiles(Path + "/dir", "*." + ext, SearchOption.TopDirectoryOnly).Select(t => t.Replace("*." + ext, ""));
        }
    }
}
